<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">商品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="tabTop">
        <!--添加与搜索-->
      <el-row :gutter="20" class="tabTop">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="initquery.query">
              <!--搜索按钮-->
            <el-button slot="append" icon="el-icon-search" @click="search()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="12">
          <el-button type="primary" @click="addgoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!--表格-->
      <el-table :data="initdate" border style="width: 100%" class="tabTop">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="350"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"  width="80"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"  width="80"></el-table-column>
        <el-table-column label="创建时间">
            <template slot-scope="scope">
                {{scope.row.add_time|dataFormat}}
            </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="tableUp(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="tableDe(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="initquery.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="initquery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
//引入删除函数
import dele from '../delet'

export default {
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.init();
  },
  data() {
    return {
      //表单数据
      initdate: [],
      //总数据条数
      total:0,
      //请求参数(分页)
      initquery: {
        query: "",  //搜索内容
        pagenum: 1,  //当前页
        pagesize: 10 //搜索条数
      }
    };
  },
  methods: {
      //搜索框
      search(){
          this.init();
      },
    //列表数据初始化
    async init() {
      let { data: res } = await this.$http.get("goods", {
        params: this.initquery
      });
      if (res.meta.status != 200) {
        return this.$message.error("请求失败");
      }
      this.$message.success("请求成功");
      this.initdate = res.data.goods;
      this.total = res.data.total;
    },
    //当前页显示是数据条数
    handleSizeChange(value){
        this.initquery.pagesize = value;
        this.init();
    },
    //当前页
    handleCurrentChange(value){
        this.initquery.pagenum = value;
        this.init();
    },
    //编辑
    tableUp(){
    },
    //删除id
    async tableDe(id){
        console.log(id);
       let a = await dele.dele(this.$confirm);
       if(a!='confirm'){
           return this.$message('取消删除');
       }
       //如果用户确认了删除
       //发送ajax请求
       let {data:res} = await this.$http.delete(`goods/${id.goods_id}`);
      
       if(res.meta.status!=200){
           console.log(res.meta);
         return this.$message.error('删除失败');
       }
        //成功就初始化函数
       this.init();
    },
    //添加商品
    addgoods(){
        this.$router.push('goods/add');
        console.log(115);
    }
  }
};
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */

.tabTop {
  margin: 10px 0;
}
</style>